package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.CraftsmanFileQO;
import com.xingkeduo.pojo.query.CraftsmanHealthQO;
import com.xingkeduo.pojo.table.CraftsmanFileTO;
import com.xingkeduo.pojo.table.CraftsmanHealthTO;
import com.xingkeduo.util.pojo.enums.YesNoEnum;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 健康证
 *
 * @author gaozifeng
 * @time 2019年3月20日19:37:32
 * @description
 */
@Component
public class CraftsmanHealthService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(CraftsmanHealthTO.class);
    private static final String NAMESPACE_FILE = CommonUtil.formatNamespace(CraftsmanFileTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanHealthTO, CraftsmanHealthQO> dao;
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<CraftsmanFileTO, CraftsmanFileQO> fileDao;

    /**
     * @param id:
     * @return : com.xingkeduo.pojo.table.CraftsmanTO
     * @author : gaozifeng
     * @date : 2019/3/20 15:35
     */
    public CraftsmanHealthTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

    /**
     * 查询
     *
     * @param query:
     * @return : java.util.List<com.xingkeduo.pojo.table.CraftsmanHealthTO>
     * @author : gaozifeng
     * @date : 2019/3/20 15:36
     */
    public List<CraftsmanHealthTO> selectByQuery(CraftsmanHealthQO query) throws Throwable {
        query.setIsDelete(YesNoEnum.NO);
        return dao.select(NAMESPACE, query);
    }

    /**
     * 查询数量
     *
     * @param query:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/3/20 15:36
     */
    public int count(CraftsmanHealthQO query) throws Throwable {
        return dao.selectCount(NAMESPACE, query);
    }

    /**
     * 新增记录
     *
     * @param craftsman:
     * @return : void
     * @author : gaozifeng
     * @date : 2019/3/20 15:42
     */
    @Transactional
    public void insert(CraftsmanHealthTO craftsman) throws Throwable {
        craftsman.initId(IdentityUtil.nextId(Worker.USER)).setCode(0);
        craftsman.setIsDelete(YesNoEnum.NO);
        if (dao.insert(NAMESPACE, craftsman) < 1) {
            throw new DataException(DataException.Type.FAIL, "新增失败");
        }

        if (null != craftsman.getFileList())
            for (CraftsmanFileTO file : craftsman.getFileList()) {
                file.setId(IdentityUtil.nextId(Worker.USER));
            }
        if (fileDao.insert(NAMESPACE_FILE, craftsman.getFileList()) < craftsman.getFileList().size()) {
            throw new DataException(DataException.Type.FAIL, "文件保存失败");
        }
    }

    @Transactional
    public void update(CraftsmanHealthTO healthTO) throws Throwable {
        if (dao.update(NAMESPACE, healthTO) < 1) {
            throw new DataException(DataException.Type.FAIL, "修改失败");
        }
    }
}
